package achievement.dao.Impl;

import achievement.dao.SzlCheckstudentArcDao;
import achievement.domain.SzlCheckstudentArc;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SzlCheckstudentArcDaoImpl implements SzlCheckstudentArcDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(SzlCheckstudentArc szlCheckstudentArc) {
        String sql = "insert into szl_checkStudentArc\n" +
                "        (student_id,arc_id,type,teacher_id,admin_id,result1,result2) \n" +
                "values(?,?,?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,szlCheckstudentArc.getStudentId());
            psmt.setLong(2,szlCheckstudentArc.getArcId());
            psmt.setInt(3,szlCheckstudentArc.getType());
            psmt.setLong(4,szlCheckstudentArc.getTeacherId());
            psmt.setLong(5,szlCheckstudentArc.getAdminId());
            psmt.setString(6,szlCheckstudentArc.getResult1());
            psmt.setString(7,szlCheckstudentArc.getResult2());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(SzlCheckstudentArc szlCheckstudentArc) {
        String sql = "update szl_checkStudentArc set";
        List<Object> param = new ArrayList<>();
        if (szlCheckstudentArc == null){
            return;
        }else{
            if(szlCheckstudentArc.getId() != null){
                sql += " id = ?,";
                param.add(szlCheckstudentArc.getId());
            }
            if(szlCheckstudentArc.getStudentId() != null){
                sql += " student_id = ?,";
                param.add(szlCheckstudentArc.getStudentId());
            }
            if(szlCheckstudentArc.getArcId() != null){
                sql += " arc_id = ?,";
                param.add(szlCheckstudentArc.getArcId());
            }
            if(szlCheckstudentArc.getType() != null){
                sql += " type = ?,";
                param.add(szlCheckstudentArc.getType());
            }
            if(szlCheckstudentArc.getTeacherId() != null){
                sql += " teacher_id = ?,";
                param.add(szlCheckstudentArc.getTeacherId());
            }
            if(szlCheckstudentArc.getAdminId() != null){
                sql += " admin_id = ?,";
                param.add(szlCheckstudentArc.getAdminId());
            }
            if(szlCheckstudentArc.getResult1() != null){
                sql += " result1 = ?,";
                param.add(szlCheckstudentArc.getResult1());
            }
            if(szlCheckstudentArc.getResult2() != null){
                sql += " result2 = ?,";
                param.add(szlCheckstudentArc.getResult2());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,szlCheckstudentArc.getId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from szl_checkStudentArc where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public SzlCheckstudentArc selectById(Long id) {
        String sql = "select * from szl_checkStudentArc where id = ?";
        SzlCheckstudentArc szlCheckstudentArc = new SzlCheckstudentArc();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                szlCheckstudentArc.setId(rs.getLong(1));
                szlCheckstudentArc.setStudentId(rs.getLong(2));
                szlCheckstudentArc.setArcId(rs.getLong(3));
                szlCheckstudentArc.setType(rs.getInt(4));
                szlCheckstudentArc.setTeacherId(rs.getLong(5));
                szlCheckstudentArc.setAdminId(rs.getLong(6));
                szlCheckstudentArc.setResult1(rs.getString(7));
                szlCheckstudentArc.setResult2(rs.getString(8));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return szlCheckstudentArc;
    }

    @Override
    public List<SzlCheckstudentArc> selectList(SzlCheckstudentArc szlCheckstudentArc) {
        String sql = "select * from szl_checkStudentArc where 1=1";
        List<SzlCheckstudentArc> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (szlCheckstudentArc == null){
            sql = "select * from szl_checkStudentArc";
        }else{
            if(szlCheckstudentArc.getId() != null){
                sql += " and id = ?";
                param.add(szlCheckstudentArc.getId());
            }
            if(szlCheckstudentArc.getStudentId() != null){
                sql += " and student_id = ?";
                param.add(szlCheckstudentArc.getStudentId());
            }
            if(szlCheckstudentArc.getArcId() != null){
                sql += " and arc_id = ?";
                param.add(szlCheckstudentArc.getArcId());
            }
            if(szlCheckstudentArc.getType() != null){
                sql += " and type = ?";
                param.add(szlCheckstudentArc.getType());
            }
            if(szlCheckstudentArc.getTeacherId() != null){
                sql += " and teacher_id = ?";
                param.add(szlCheckstudentArc.getTeacherId());
            }
            if(szlCheckstudentArc.getAdminId() != null){
                sql += " and admin_id = ?";
                param.add(szlCheckstudentArc.getAdminId());
            }
            if(szlCheckstudentArc.getResult1() != null){
                sql += " and result1 = ?";
                param.add(szlCheckstudentArc.getResult1());
            }
            if(szlCheckstudentArc.getResult2() != null){
                sql += " and result2 = ?";
                param.add(szlCheckstudentArc.getResult2());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                SzlCheckstudentArc szlCheckstudentArc1 = new SzlCheckstudentArc();
                szlCheckstudentArc1.setId(rs.getLong(1));
                szlCheckstudentArc1.setStudentId(rs.getLong(2));
                szlCheckstudentArc1.setArcId(rs.getLong(3));
                szlCheckstudentArc1.setType(rs.getInt(4));
                szlCheckstudentArc1.setTeacherId(rs.getLong(5));
                szlCheckstudentArc1.setAdminId(rs.getLong(6));
                szlCheckstudentArc1.setResult1(rs.getString(7));
                szlCheckstudentArc1.setResult2(rs.getString(8));
                list.add(szlCheckstudentArc1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
